package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.Address;
import cn.gjculture.shopping.api.entity.PageInfo;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.service.AddressService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    @ApiOperation(value = "新增地址", notes = "新增地址")
    @ApiImplicitParam(name = "address", value = "地址对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(Address address) {
        Return ret = new Return();
        if(addressService.insert(address)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "设置默认地址", notes = "设置默认地址")
    @ApiImplicitParam(name = "addressId", value = "地址ID", required = true, dataType = "String",paramType = "path")
    @PutMapping(value = "/setDefault/{addressId}")
    public Return setDefault(@PathVariable(value = "addressId") String addressId){
        Return ret=new Return();
        addressService.setDefault(addressId);
        ret.getCommon().setRet(true);
        return ret;
    }

    @ApiOperation(value = "修改地址", notes = "修改地址")
    @ApiImplicitParam(name = "activity", value = "地址对象", required = true, dataType = "Object")
    @PutMapping
    public Return<Address> update(Address address){
        Return<Address> ret=new Return<>();
        if(addressService.update(address)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "获取地址列表", notes = "获取地址列表")
    @GetMapping
    public Return<List<Address>> query(PageInfo pageInfo) {
        Return<List<Address>> ret = new Return<>();
        List<Address> addresses = addressService.query();
        ret.setData(addresses);
        return ret;
    }
    @ApiOperation(value = "获取地址", notes = "获取地址")
    @ApiImplicitParam(name = "customerId", value = "用户ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/byCustomer/{customerId}")
    public Return<List<Address>> queryByCustomerId(@PathVariable(value = "customerId") String customerId) {
        Return<List<Address>> ret = new Return<>();
        List<Address> addresss = addressService.queryByCustomerId(customerId);
        ret.setData(addresss);
        return ret;
    }



    @ApiOperation(value = "获取地址", notes = "获取地址")
    @ApiImplicitParam(name = "addressId", value = "地址ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{addressId}")
    public Return<Address> getAddressById(@PathVariable(value = "addressId") String addressId) {
        Return<Address> ret = new Return<>();
        Address address = addressService.queryById(addressId);
        ret.setData(address);
        ret.getCommon().setRet(true);
        return ret;
    }


    @ApiOperation(value = "删除地址", notes = "删除地址")
    @ApiImplicitParam(name = "addressIds", value = "地址ID", required = true, dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{addressIds}")
    public Return<Address> delete(@PathVariable(value = "addressIds") List<String> addressIds) {
        Return<Address> ret = new Return<>();
        if(addressService.deleteMore(addressIds)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }


}
